import axios from "axios";
import qs from "qs";
let query = require("querystring");
// 支持生产环境和开发环境的API配置
let base = process.env.VUE_APP_BASE_API || '/api';
console.log('API Base URL:' + base)

// 生产环境API配置
if (process.env.NODE_ENV === 'production') {
    // 生产环境：可以通过环境变量或配置文件设置API地址
    base = process.env.VUE_APP_API_URL || '/api';
}
// 获取相对路径
function getProPath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符
    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    relUrl = relUrl.split("/")[1];
    return relUrl;
}
let road = "/" + getProPath(); //路径

road = ""; //路径 打包时 注释 
export let serverUrl = base + road;
// console.log(serverUrl)
export let pageSize = 10

// 获取验证码
// export const getVcode = () => {
//     return axios.get(`${base}${road}/admin/captcha`);
// };
// 登录
export const loginWeb = (params) => {
    return axios.post(`${base}${road}/admin/login`, params);
};



// 登出
export const logout = () => {
    return axios.get(`${base}${road}/admin/loginOut`);
};
//首页 统计数据
export const getHomeData = () => {
    return axios.get(`${base}${road}/api/homepage/statistics`);
};
//首页 轮播图
export const carousel = () => {
    return axios.get(`${base}${road}/api/homepage/carousel`);
};
//开发者中心页面数据
export const getKaifaData = () => {
    return axios.get(`${base}${road}/developerCenter/summary`);
};
// 机器人列表
export const getSelectByType = (params) => {
    return axios.get(`${base}${road}/rpacontroller/selectByType?${qs.stringify(params)}`);
};

// 机器人-详情
export const getRpaInfo = params => {
    return axios.get(`${base}${road}/rpacontroller/selectByRpaId?${qs.stringify(params)}`);
};
// 机器人-模糊搜索
export const searchRpaRobots = (params) => {
    return axios.get(`${base}${road}/rpacontroller/search?${qs.stringify(params)}`);
};
// 机器人-删除
export const deleteRpa = params => {
    return axios.get(`${base}${road}/rpacontroller/delete?${qs.stringify(params)}`);
};
// 保存信息
export const saveRpacontroller = (id, params) => {
    return axios.post(`${base}${road}/rpacontroller/addNew/${id}`, params);
};
// 删除图片
export const removeImgFun = (params) => {
    return axios.get(`${base}${road}/rpaFileUpload/${params}/removeScreenShot`);
};
// 删除ibt
export const removeIbtFun = (params) => {
    return axios.get(`${base}${road}/rpaFileUpload/${params}/remove`);
};
// 业务类型
export const bizTypeMenu = () => {
    return axios.get(`${base}${road}/constants/menuList`);
};








// 获取当前用户信息
export const getMyUserInfo = () => {
    return axios.get(`${base}${road}/web/user/getLoginUser`);
};

//   修改密码
export const updatePwd = params => {
    return axios.post(`${base}${road}/web/user/updatePassword`, params);
};

// select:组织机构
export const orgSelectApi = () => {
    return axios.get(`${base}${road}/web/user/getOrganizationOption`);
};
// select:用户类型
export const userTypeSelectApi = () => {
    return axios.get(`${base}${road}/web/user/getUserTypeOption`);
};
// select:用户角色
export const roleSelectApi = () => {
    return axios.get(`${base}${road}/web/user/getWebRoleOption`);
};
// select:工单状态
export const orderStatusSelectApi = () => {
    return axios.post(`${base}${road}/web/order/getOrderStatusOption`);
};
// select:价目管理-外包项目选项
export const orderItemSelectApi = () => {
    return axios.get(`${base}${road}/web/orderItem/getOrderItemOption`);
};
// select:价目管理-外包项目结算单位
export const orderItemUnitSelectApi = () => {
    return axios.get(`${base}${road}/web/orderItem/getOrderItemUnitOption`);
};
// select:价目管理-业务类型
export const orderItemTypeSelectApi = () => {
    return axios.get(`${base}${road}/web/orderItem/getOrderItemTypeOption`);
};
// select:分公司
export const companySelectApi = () => {
    return axios.get(`${base}${road}/web/order/getCompanyOption`);
};
// 系统管理-用户管理-列表
export const getUserInfoList = (params) => {
    return axios.get(`${base}${road}/web/user/getUserInfoList?${qs.stringify(params)}`);
};
// 系统管理-用户管理-保存
export const saveUserInfo = params => {
    return axios.post(`${base}${road}/web/user/saveUserInfo`, params);
};
// 系统管理-用户管理-删除
export const removeUserInfo = id => {
    return axios.delete(`${base}${road}/web/user/removeUserInfo/${id}`);
};
// 系统管理-用户管理-解锁账号
export const unlockAccount = params => {
    return axios.post(`${base}${road}/web/user/unlockAccount?${qs.stringify(params)}`);
};
// 系统管理-用户管理-详情
export const getUserInfo = id => {
    return axios.get(`${base}${road}/web/user/getUserInfo/${id}`);
};
// 系统管理-组织机构管理-列表
export const getOrganizationList = (params) => {
    return axios.get(`${base}${road}/web/organization/getOrganizationList?${qs.stringify(params)}`);
};
// 系统管理-组织机构管理-同步数据
export const asyncOrganizationData = (params) => {
    return axios.get(`${base}${road}/web/organization/asyncOrganizationData`);
};
// 系统管理-组织机构管理-设置业务编号
export const saveOrganizationBusinessNo = (params) => {
    return axios.post(`${base}${road}/web/organization/saveOrganizationBusinessNo`, params);
};
// 价目管理-列表
export const getOrderItemList = (params) => {
    return axios.get(`${base}${road}/web/orderItem/getOrderItemList?${qs.stringify(params)}`);
};
// 价目管理-详情
export const getOrderItemInfo = id => {
    return axios.get(`${base}${road}/web/orderItem/getOrderItemInfo/${id}`);
};
// 价目管理-删除
export const removeOrderItem = id => {
    return axios.delete(`${base}${road}/web/orderItem/removeOrderItem/${id}`);
};
// 价目管理-保存
export const saveOrderItem = params => {
    return axios.post(`${base}${road}/web/orderItem/saveOrderItem`, params);
};
// 系统管理-企信用户管理-列表
export const getWechatUserList = (params) => {
    return axios.get(`${base}${road}/web/user/getWechatUserList?${qs.stringify(params)}`);
};
// 系统管理-企信用户管理-同步VM用户
export const asyncUserData = () => {
    return axios.get(`${base}${road}/web/user/asyncUserData`);
};
// 系统管理-企信用户管理-批量指定用户为特定人员
export const appointUserToSpecial = params => {
    return axios.post(`${base}${road}/web/user/appointUserToSpecial`, params);
};
// 系统管理-企信用户管理-批量取消用户特定人员指定
export const cancelUserSpecialAppoint = params => {
    return axios.post(`${base}${road}/web/user/cancelUserSpecialAppoint`, params);
};
// 系统管理-企信用户管理-批量指定用户为评价人员
export const appointUserToEvaluator = params => {
    return axios.post(`${base}${road}/web/user/appointUserToEvaluator/${params}`);
};
// 系统管理-企信用户管理-批量取消用户评价人员指定
export const cancelUserEvaluatorAppoint = params => {
    return axios.post(`${base}${road}/web/user/cancelUserEvaluatorAppoint/${params}`);
};

// 系统管理-组织机构指标数据-列表
export const getOrganizationTargetList = (params) => {
    return axios.get(`${base}${road}/web/organizationTarget/getOrganizationTargetList?${qs.stringify(params)}`);
};
// 系统管理-组织机构指标数据-删除
export const removeOrganizationTargetData = id => {
    return axios.post(`${base}${road}/web/organizationTarget/removeOrganizationTargetData/${id}`);
};
// 系统管理-组织机构指标数据-保存
export const saveOrganizationTargetData = params => {
    return axios.post(`${base}${road}/web/organizationTarget/saveOrganizationTargetData`, params);
};










// 工单统计-工单统计列表
export const getOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/getOrderStatistic?${qs.stringify(params)}`);
};
// 工单统计-工单统计 导出
export const exportOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/exportOrderStatistic?${qs.stringify(params)}`);
};
// 工单统计-工单统计 导出
export const getOrderInfo = (params) => {
    return axios.get(`${base}${road}/web/order/getOrderInfo/${params}`);
};

// 工单统计-个人排班统计列表
export const getPersonalOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/getPersonalOrderStatistic?${qs.stringify(params)}`);
};
// 工单统计-个人排班统计 导出
export const exportPersonalOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/exportPersonalOrderStatistic?${qs.stringify(params)}`);
};
// 工单查询 导出
export const getOrderList = (params) => {
    return axios.get(`${base}${road}/web/order/getOrderList?${qs.stringify(params)}`);
};


//  急抢修评价统计----列表
export const getEmergencyOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/getEmergencyOrderStatistic?${qs.stringify(params)}`);
};
// 急抢修评价统计----导出
export const exportEmergencyOrderStatistic = (params) => {
    return axios.get(`${base}${road}/web/order/exportEmergencyOrderStatistic?${qs.stringify(params)}`);
};

// ==================== 业务类型管理接口 ====================

// 获取所有业务类型
export const getBizTypeList = () => {
    return axios.get(`${base}${road}/admin/biz-type/list`);
};

// 根据ID获取业务类型
export const getBizTypeById = (id) => {
    return axios.get(`${base}${road}/admin/biz-type/${id}`);
};

// 创建业务类型
export const createBizType = (data) => {
    return axios.post(`${base}${road}/admin/biz-type/create`, data);
};

// 更新业务类型
export const updateBizType = (data) => {
    return axios.put(`${base}${road}/admin/biz-type/update`, data);
};

// 删除业务类型
export const deleteBizType = (id) => {
    return axios.delete(`${base}${road}/admin/biz-type/${id}`);
};

// 获取所有子业务类型
export const getSubBizTypeList = () => {
    return axios.get(`${base}${road}/admin/biz-type/sub/list`);
};

// 根据父类型ID获取子业务类型
export const getSubBizTypesByParentId = (parentTypeId) => {
    return axios.get(`${base}${road}/admin/biz-type/sub/parent/${parentTypeId}`);
};

// 根据ID获取子业务类型
export const getSubBizTypeById = (id) => {
    return axios.get(`${base}${road}/admin/biz-type/sub/${id}`);
};

// 创建子业务类型
export const createSubBizType = (data) => {
    return axios.post(`${base}${road}/admin/biz-type/sub/create`, data);
};

// 更新子业务类型
export const updateSubBizType = (data) => {
    return axios.put(`${base}${road}/admin/biz-type/sub/update`, data);
};

// 删除子业务类型
export const deleteSubBizType = (id) => {
    return axios.delete(`${base}${road}/admin/biz-type/sub/${id}`);
};

// 获取业务类型树
export const getBizTypeTree = () => {
    return axios.get(`${base}${road}/admin/biz-type/tree`);
};

// 检查业务类型编码是否存在
export const checkBizTypeCode = (typeCode, excludeId) => {
    let url = `${base}${road}/admin/biz-type/check-code?typeCode=${typeCode}`;
    if (excludeId && excludeId !== null && excludeId !== 'null') {
        url += `&excludeId=${excludeId}`;
    }
    return axios.get(url);
};

// 检查子业务类型编码是否存在
export const checkSubBizTypeCode = (typeCode, excludeId) => {
    let url = `${base}${road}/admin/biz-type/sub/check-code?typeCode=${typeCode}`;
    if (excludeId && excludeId !== null && excludeId !== 'null') {
        url += `&excludeId=${excludeId}`;
    }
    return axios.get(url);
};

// ==================== 账号安全模块接口 ====================

// 更新用户资料
export const updateUserProfile = (data) => {
    return axios.post(`${base}${road}/account/updateProfile`, data);
};

// 修改密码
export const changePassword = (data) => {
    return axios.post(`${base}${road}/account/changePassword`, data);
};

// ==================== 统计模块接口 ====================

// 获取统计概览数据
export const getStatisticsOverview = () => {
    return axios.get(`${base}${road}/statistics/overview`);
};

// 获取大业务类型分布
export const getBizTypeDistribution = () => {
    return axios.get(`${base}${road}/statistics/biz-type-distribution`);
};

// 获取细分业务类型分布
export const getSubBizTypeDistribution = () => {
    return axios.get(`${base}${road}/statistics/sub-biz-type-distribution`);
};

// 获取下载量排行榜
export const getDownloadRanking = (limit = 10) => {
    return axios.get(`${base}${road}/statistics/download-ranking?limit=${limit}`);
};

// 获取浏览量排行榜
export const getViewRanking = (limit = 10) => {
    return axios.get(`${base}${road}/statistics/view-ranking?limit=${limit}`);
};

// 获取开发者排行榜
export const getDeveloperRanking = (limit = 10) => {
    return axios.get(`${base}${road}/statistics/developer-ranking?limit=${limit}`);
};

// 获取下载量趋势
export const getDownloadTrend = (timeRange = 'daily') => {
    return axios.get(`${base}${road}/statistics/download-trend?timeRange=${timeRange}`);
};

// 获取浏览量趋势
export const getViewTrend = (timeRange = 'daily') => {
    return axios.get(`${base}${road}/statistics/view-trend?timeRange=${timeRange}`);
};

// ==================== 精选应用模块接口 ====================

// 获取精选应用列表
export const getFeaturedApps = (params) => {
    return axios.get(`${base}${road}/rpacontroller/featured?${qs.stringify(params)}`);
};

// 设置/取消精选应用
export const updateFeaturedStatus = (id, isFeatured) => {
    return axios.put(`${base}${road}/rpacontroller/featured/${id}?isFeatured=${isFeatured}`);
};

// 获取精选应用统计
export const getFeaturedAppsStatistics = () => {
  return axios.get(`${base}${road}/rpacontroller/featured/statistics`);
};

// ==================== 开发者中心搜索模块接口 ====================

// 搜索用户应用
export const searchUserRobots = (params) => {
  return axios.get(`${base}${road}/rpacontroller/search/user?${qs.stringify(params)}`);
};






